package com.hexiang.err;

import com.hexiang.domain.CommonResponse;
import com.hexiang.exception.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 处理其他异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object exceptionHandler(Exception e) {
        log.error("application occur error :", e);
        return Mono.just(CommonResponse.builder().code(500).msg(e.getMessage()).succ(false).build());

    }

    @ExceptionHandler(value = ClientException.class)
    @ResponseBody
    public Object ClientExceptionHandler(ClientException e) {
        log.warn("client abort :", e);
        return Mono.just(CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build());
    }

}